<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/mytag.jspf"%>

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<title>用户</title>
<style>
</style>
<script type="text/javascript">
$("#divId").hide();
$("#purpose").change(function(){
	if($(this).val()=="KQ"){
		$("#type").val("TW");
		$("#divId").show();
	}else{
		$("#type").val("TP");
		$("#divId").hide();
	}
});
function upImage(){
	$("#imgForm").ajaxSubmit({
		dataType : "json",
		success : function(data) {
			if (data.success == true) {
				var url = data.msg;
				$("#imgId").attr("src",url);
				$("#img").val(url);
			} else {
				alert(data.msg);
			}
		}
	});
}
$(function(){
	$("#form").validate({
		//ignore:".ignore",//出去class = ignore的表单不验证，其他的隐藏表单都可以验证
		rules : {
			"purpose" : {
				required : true,
				remote : { // 异步验证是否存在
					type : "POST",
					url : 'management/material/isExist',
					data : {
						type : function() {
							return $("#purpose").val();
						}
					}
				}
			},
			"name" : {
				required : true
			},
			"shopName" : {
				required : true
			},
			"img" :{
				required : true
			},
			"shopName" : {
				required : true
			},
			"title" : {
				required : true
			},
			"content" : {
				required : true
			},
			"summary" : {
				required : true
			}
		},
		messages : {
			"purpose" : {
				required : "请选择类型",
				remote : "生日关怀类型已经存在一条记录，该类型有且只能有一条记录"
			},
			"shopName" : {
				required : "请选择门店"
			},
			"img" :{
				required:"请选择上传素材图片"
			},
			"shopName" : {
				required : "请选择门店"
			},
			"title" : {
				required : "请填写素材标题"
			},
			"content" : {
				required : "请填写素材内容"
			},
			"summary" : {
				required : "请填写素材摘要"
			}
		},
		errorPlacement : function(error, element) {//错误信息显示位置
			if (element.attr("name") == "img") {
				error.css("left", $("#upBtn").width() + 30);
				error.appendTo($("button[name='upBtn']"));
			} else {
				error.insertAfter(element);
			}
		}
	});
	
	
	$("#submitBtn").on("click",function(){
		var flag = $("#form").valid();
		if(flag == true){
			$("#form").submit();
			$("#submitBtn").off("click");
		}
	});
});
</script>
</head>
<body>
	<ol class="am-breadcrumb am-breadcrumb-slash am-padding" style="margin-bottom: 0px;">
		<li><a href="management/index">首页</a></li>
		<li><a href="management/material/index">素材信息维护</a></li>
		<li class="am-active">新增</li>
	</ol>
	<div class="am-g">
		<div class="am-u-md-12">
				<div class="am-tabs am-margin">
					<ul class="am-tabs-nav am-nav am-nav-tabs">
						<li class="am-active"><a href="javascript:void(0)">基本信息</a></li>
					</ul>
					<div class="am-tabs-bd">
						<div class="am-tab-panel am-active">
							<div>
								<div class="am-u-sm-12">
									<form id="imgForm" class="am-form am-form-horizontal am-block" action="management/file" method="post" enctype="multipart/form-data">
										<label class="am-u-sm-2 am-form-label">素材图片</label>
										<div class="am-u-sm-4 am-u-end">
											<img id="imgId" src="file/a.png" class="am-form-field">
											<div class="am-form-group am-form-file">
												<button id="upBtn" name="upBtn" type="button" class="am-btn am-btn-default">
													<i class="am-icon-cloud-upload"></i> 选择要上传的图片
												</button>
												<input name="imageFile" id="imageFile" onchange="upImage();" type="file" multiple>
											</div>
										</div>
									</form>
								</div>
								<div class="am-u-sm-12">
									<form class="am-form am-form-horizontal" id="form" name="form"  method="post" action="management/material/save">
										<input  type="text" name="img" id="img" style="height:0; width:0; border:0;visibility: hidden"> 
										<!-- <input  type="hidden" name="img" id="img">-->
										<div class="am-form-group">
											<label class="am-u-sm-2 am-form-label">素材名称</label>
											<div class="am-u-md-4 am-u-end">
												<input type="text" id="name" name="name" maxlength="100" class="am-form-field am-input-sm">
											</div>
										</div>
										<div class="am-form-group">
											<label class="am-u-sm-2 am-form-label">素材用途</label>
											<div class="am-u-md-4 am-u-end">
												<t:dictSelect   tabName="MATERIAL" colName="PURPOSE" field="purpose" id="purpose" hasEmptyOption="true"></t:dictSelect>
												<input type="hidden" name="type" id="type" class="am-form-field am-input-sm">
											</div>
										</div>
										<div id="divId">
										<div class="am-form-group">
											<label class="am-u-sm-2 am-form-label">素材标题</label>
											<div class="am-u-md-4 am-u-end">
												<input type="text" id=""title"" name="title" maxlength="50" class="am-form-field am-input-sm">
											</div>
										</div>
										<div class="am-form-group">
											<label class="am-u-sm-2 am-form-label">素材内容</label>
											<div class="am-u-md-4 am-u-end">
												<textarea id="content" name="content"  maxlength="120" class="am-form-field am-input-sm">
												</textarea>
											</div>
										</div>
										<div class="am-form-group">
											<label class="am-u-sm-2 am-form-label">素材摘要</label>
											<div class="am-u-md-4 am-u-end">
												<textarea id="summary" name="summary"  maxlength="120" class="am-form-field am-input-sm">
												</textarea>
											</div>
										</div>
										</div>
										
									</form>	
								</div>
							</div>	
						</div>
					</div>
				</div>
				<div class="am-margin">
					<button class="am-btn am-btn-primary am-btn-xs" type="button" id="submitBtn"><span class="am-icon-check"></span> 保存</button>
					<a href="javascript:;" onClick="javascript:history.back(-1);" class="am-btn am-btn-primary am-btn-xs"><span class="am-icon-times"></span> 取消</a>
				</div>
		</div>
	</div>
</body>
</html>
